/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.job;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVParser;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Slf4j
public class DBWriter implements ItemWriter {

    private JdbcTemplate jdbcTemplate;

    public DBWriter(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void write(List items) throws Exception {
        Object oob = items.get(0);
        String sql = "INSERT INTO PEOPLE(first_name, last_name) VALUES(?, ?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                log.info("Insert data");

                ps.setString(1, "first_name");
                ps.setString(2, "last_name");
            }

            @Override
            public int getBatchSize() {
                return items.size();
            }
        });
    }
}
